import React from 'react';
import { inject, observer } from 'mobx-react';
import {Button,Spin,Modal} from 'antd';
import { WeaDialog,WeaNewScroll,WeaFormItem,WeaLocaleProvider,WeaSearchGroup,WeaInput,WeaBrowser,WeaDatePicker,WeaTools } from 'ecCom';
import SelectTab from '../../public/selectTab';
const getLabel = WeaLocaleProvider.getLabel;
@inject('devDoorpage')
@observer
export default class DoorAddTask extends React.Component {
    constructor(props) {
        super(props);
        this.state={
            spinning:false,
            params: {
                taskName: '',
                enddate: '',
                remark: '',
                principalid: [],
                parentid: [],
                datetype: '',
                partnerid: [],
                sorttype:'5'
            },
            sTab : [
                {
                    classstyle: 'workrelate-task-slink',
                    name: getLabel(15537,'今日'),
                    id: 1
                },
                {
                    classstyle: 'workrelate-task-slink',
                    name: getLabel(22488,'明天'),
                    id: 2
                },
                {
                    classstyle: 'workrelate-task-slink',
                    name: getLabel(382509,'即将'),
                    id: 3
                },
                {
                    classstyle: 'workrelate-task-slink workrelate-task-sdlink',
                    name: getLabel(382499,'不标记'),
                    id: 4
                },
                {
                    classstyle: 'workrelate-task-slink',
                    name: getLabel(382498,'备忘'),
                    id: 5
                }
            ]
		}
    }
    getButton = (ecId)=>{
        let buttonsCreate = [
            (<Button ecId={`${ecId}_Button@23lrlx@save`} type="primary" onClick={this.doSubmit}>{getLabel(86,'保存')}</Button>),
            (<Button ecId={`${ecId}_Button@ou2fup@saveandnew`} type="primary" onClick={this.doSubmitAndNew}>{getLabel(32720,'保存并新建')}</Button>),
            (<Button ecId={`${ecId}_Button@uo3m6h@close`} type="ghost" onClick={this.onlyClose}>{getLabel(309,'关闭')}</Button>)
        ];
        return buttonsCreate;
    }
    onlyClose=()=>{
        this.cleanValues();
        this.props.onlyClose && this.props.onlyClose();
    }
    doClose(){
        this.cleanValues();
        this.props.doClose && this.props.doClose();
    }
    doRefresh(){
        this.cleanValues();
        this.props.doRefresh && this.props.doRefresh();
    }
    cleanValues(){
        this.setState({
            spinning:false,
            params: {
                taskName: '',
                enddate: '',
                remark: '',
                principalid: [],
                parentid: [],
                datetype: '',
                partnerid: [],
                sorttype:'5'
            },
            sTab : [
                {
                    classstyle: 'workrelate-task-slink',
                    name: getLabel(15537,'今日'),
                    id: 1
                },
                {
                    classstyle: 'workrelate-task-slink',
                    name: getLabel(22488,'明天'),
                    id: 2
                },
                {
                    classstyle: 'workrelate-task-slink',
                    name: getLabel(382509,'即将'),
                    id: 3
                },
                {
                    classstyle: 'workrelate-task-slink workrelate-task-sdlink',
                    name: getLabel(382499,'不标记'),
                    id: 4
                },
                {
                    classstyle: 'workrelate-task-slink',
                    name: getLabel(382498,'备忘'),
                    id: 5
                }
            ]
        });
    }
    doSubmitAndNew=()=>{
        let alldatas = this.state.params;
        let canSave = true;
        for(let key in alldatas){
            if(key == 'taskName' && alldatas[key] == ''){//标题为空
                canSave = false;
            }
            if(key=='principalid' || key=='parentid' || key=='partnerid'){
                let str ='',arr=[];
				alldatas[key].forEach((element)=>{
					arr.push(element.id)
				})
				alldatas = {...alldatas,[key]:arr.toString()}
            }
        }
        if(!canSave){
            Modal.warning({
                title: getLabel(558,'提示'),
                content: getLabel(504646,'任务名称不能为空'),
            });
        }else{
            this.setState({spinning:true});
            canSave && WeaTools.callApi('/api/workrelate/task/addTask', 'POST', alldatas).then((data)=>{
                if(data.status==1){
                    this.doRefresh();//清空表单数据并刷新
                }else{
                    Modal.warning({
                        title: getLabel(558,'提示'),
                        content: getLabel(508744,'保存出错,请重新尝试或联系管理员'),
                    });
                    this.setState({spinning:false});
                }
            });
        }
    }
    doSubmit=()=>{
        let alldatas = this.state.params;
        let canSave = true;
        for(let key in alldatas){
            if(key == 'taskName' && alldatas[key] == ''){//标题为空
                canSave = false;
            }
            if(key=='principalid' || key=='parentid' || key=='partnerid'){
                let str ='',arr=[];
				alldatas[key].forEach((element)=>{
					arr.push(element.id)
				})
				alldatas = {...alldatas,[key]:arr.toString()}
            }
        }
        if(!canSave){
            Modal.warning({
                title: getLabel(558,'提示'),
                content: getLabel(504646,'任务名称不能为空'),
            });
        }else{
            this.setState({spinning:true});
            canSave && WeaTools.callApi('/api/workrelate/task/addTask', 'POST', alldatas).then((data)=>{
                if(data.status==1){
                    this.doClose();//关闭弹出框
                }else{
                    Modal.warning({
                        title: getLabel(558,'提示'),
                        content: getLabel(508744,'保存出错,请重新尝试或联系管理员'),
                    });
                    this.setState({spinning:false});
                }
            });
        }
    }
    setTodo(id,idx,type){
        let sTab  = this.state.sTab;
        for(var i=0;i<sTab.length;i++){
            if(i==idx){
                sTab[i].classstyle = "workrelate-task-slink workrelate-task-sdlink";
            }else{
                sTab[i].classstyle = "workrelate-task-slink";
            }
        }
        let { params } = this.state;
        params = { ...params, [type]: id };
        this.setState({ params: params,sTab:sTab });
    }
    onInputChange = (values, type) => {
        let { params } = this.state;
        params = { ...params, [type]: values };
        this.setState({ params: params });
    }
    onChangeBrowser = (ids, names, datas, type) => {
        let { params } = this.state;
        params = { ...params, [type]: datas }
        this.setState({ params: params })
    }
    render() {
        let {spinning,params,sTab} = this.state;
        const { devDoorpage } = this.props;
        return (
            <WeaDialog ecId={`${this && this.props && this.props.ecId || ''}_WeaDialog@0g3gys`}
				visible={devDoorpage.status.createvisable}
				title={this.props.title}
				icon="icon-coms-Journal"
				iconBgcolor="#7734FF"
				className="createWorkPlan"
				buttons={this.getButton(this.props.ecId || '')}
                style={this.props.style}
                closable= {true}
				onCancel={this.onlyClose}
			>
			    <Spin ecId={`${this && this.props && this.props.ecId || ''}_Spin@419vcx`} spinning={spinning} style={{height:'100%'}}>
                    <WeaNewScroll ecId={`${this && this.props && this.props.ecId || ''}_WeaNewScroll@rbovpx`} height={this.props.style.height}>
                        <WeaSearchGroup ecId={`${this && this.props && this.props.ecId || ''}_WeaSearchGroup@r86kcc`} title={getLabel(1361,'基本信息')} needTigger={true} showGroup={true}>
                            <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@z0dvci`}
                                label={getLabel(195,'名称')}
                                labelCol={{span: 4}}
                                wrapperCol={{span: 18}}>
                                <WeaInput ecId={`${this && this.props && this.props.ecId || ''}_WeaInput@3j4dfu`} onChange = {(value) => this.onInputChange(value,"taskName")}
                                placeholder={getLabel(508736,'请输入任务名称')} viewAttr={3} value={params.taskName}
                                />
                            </WeaFormItem>
                            <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@0s982n`}
                                label={getLabel(382534,'标记Todo')}
                                labelCol={{span: 4}}
                                wrapperCol={{span: 18}}>
                                <SelectTab ecId={`${this && this.props && this.props.ecId || ''}_SelectTab@uhg0z3`} dataSource={sTab} spanClass="workrelate-task-spanClass" onClick={(value,idx) => this.setTodo(value,idx,"datetype")}/>
                            </WeaFormItem>
                            <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@sza26m`}
                                label={getLabel(23785,'上级任务')}
                                labelCol={{span: 4}}
                                wrapperCol={{span: 18}}>
                                 <WeaBrowser ecId={`${this && this.props && this.props.ecId || ''}_WeaBrowser@iho8br`}
                                    onChange={(ids, names, datas) => {this.onChangeBrowser(ids, names, datas,"parentid")}}
                                    replaceDatas = {params.parentid}
                                    title={getLabel(23785,'上级任务')}
                                    type="wrltsk"
                                    viewAttr={2}
                                    inputStyle={{ width: 242}} 
                                    linkUrl ="/spa/workrelate/static/index.html?#/main/workrelate/task/taskDetail?id="
                                />
                            </WeaFormItem>
                            <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@xxcrn0`}
                                label={getLabel(433,'描述')}
                                labelCol={{span: 4}}
                                wrapperCol={{span: 18}}>
                                <WeaInput ecId={`${this && this.props && this.props.ecId || ''}_WeaInput@zswhxq`} id="remark" onChange = {(value) =>  this.onInputChange(value,"remark")} value={params.remark}
                                placeholder={getLabel(508733,'请输入任务描述')}/>
                            </WeaFormItem>
                            <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@dbj6vq`}
                                label={getLabel(84043,'责任人')}
                                labelCol={{span: 4}}
                                wrapperCol={{span: 18}}>
                                <WeaBrowser ecId={`${this && this.props && this.props.ecId || ''}_WeaBrowser@9d9vf6`}
                                    type={1}
                                    replaceDatas = {params.principalid}
                                    inputStyle={{ width: 242 }}
                                    onChange={(ids, names, datas)=> this.onChangeBrowser(ids, names, datas,"principalid")}
                                />
                            </WeaFormItem>
                            <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@qojjit`}
                                label={getLabel(382557,'参与人')}
                                labelCol={{span: 4}}
                                wrapperCol={{span: 18}}>
                                <WeaBrowser ecId={`${this && this.props && this.props.ecId || ''}_WeaBrowser@7ixxe5`}
                                    type={17}
                                    replaceDatas = {params.partnerid}
                                    inputStyle={{width: 242}}
                                    onChange={(ids, names, datas)=> this.onChangeBrowser(ids, names, datas,"partnerid")}
                                    isSingle={false}
                                />
                            </WeaFormItem>
                            <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@14tkoj`}
                                label={getLabel(743,'结束时间')}
                                labelCol={{span: 4}}
                                wrapperCol={{span: 18}}>
                                <WeaDatePicker ecId={`${this && this.props && this.props.ecId || ''}_WeaDatePicker@s7ilff`} value={params.enddate}
                                    onChange={value =>this.onInputChange(value,"enddate")}
                                />
                            </WeaFormItem>
                        </WeaSearchGroup >
                    </WeaNewScroll>
			    </Spin>
			</WeaDialog>
        )
    }
};